package com.pzh.assignment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzh.assignment.common.bean.Result;
import com.pzh.assignment.common.bean.param.RoleParam;
import com.pzh.assignment.common.bean.vo.RoleVO;
import com.pzh.assignment.common.shiro.ShiroUtils;
import com.pzh.assignment.entity.Groups;
import com.pzh.assignment.entity.Role;
import com.pzh.assignment.entity.User;
import com.pzh.assignment.service.IRoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pengzhihao
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @RequiresRoles(value = {"sys_admin"}, logical = Logical.OR)
    @GetMapping
    public Result listRoleByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "5") Integer size) {
        Page<Role> page = new Page<>(currentPage, size);
        QueryWrapper<Role> roleQueryWrapper = initRoleWrapper();
        IPage pageData = roleService.page(page, roleQueryWrapper);
        List<RoleVO> voList = ((IPage<Role>) pageData).getRecords().stream().map(RoleVO::valueOf).collect(Collectors.toList());
        pageData.setRecords(voList);
        return Result.success(pageData);
    }

    @GetMapping("/total")
    public Result totalRole() {
        QueryWrapper<Role> roleQueryWrapper = initRoleWrapper();
        List<Role> roles = roleService.list(roleQueryWrapper);
        List<RoleVO> voList = roles.stream().map(RoleVO::valueOf).collect(Collectors.toList());
        return Result.success(voList);
    }

    @RequiresRoles(value = {"sys_admin"}, logical = Logical.OR)
    @PostMapping
    public Result saveOrUpdate(@Validated @RequestBody RoleParam param) {
        Role role = new Role();
        role.setName(param.getName());
        role.setAlias(param.getAlias());
        role.setWeight(param.getWeight());
        if (param.getId() != null && param.getId() != -1){
            role.setId(param.getId());
        }
        roleService.saveOrUpdate(role);
        return Result.success(null);
    }

    @RequiresRoles(value = {"sys_admin"}, logical = Logical.OR)
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        roleService.removeById(id);
        return Result.success(null);
    }

    private QueryWrapper<Role> initRoleWrapper(){
        Role role = ShiroUtils.getProfile().getRole();
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        switch (role.getName()) {
            case Role.SYSTEM_ADMIN:{
                wrapper.orderByDesc("create_time");
                break;
            }
            case Role.SUPER_ADMIN:{
                wrapper.ge("weight", role.getWeight()).orderByDesc("create_time");
                break;
            }
            default:{
                wrapper.gt("weight", role.getWeight()).orderByDesc("create_time");
                break;
            }
        }
        return wrapper;
    }


}
